package org.plex.core.imp;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.plex.core.RoleManager;
import org.plex.model.Role;
import org.plex.model.imp.RoleImp;
import org.plex.searchfilter.imp.RoleSearchFilter;

import com.adobe.idp.dsc.clientsdk.ServiceClientFactory;
import com.adobe.idp.um.api.AuthorizationManager;
import com.adobe.idp.um.api.UMException;
import com.adobe.livecycle.usermanager.client.AuthorizationManagerServiceClient;

public class RoleManangerImp implements RoleManager, Serializable
{
	private static final long		serialVersionUID	= -3824722902637214400L;
	private ServiceClientFactory	serviceClientFactory;
	private AuthorizationManager	authorizationManager;

	public RoleManangerImp(ServiceClientFactory serviceClientFactory)
			throws NullPointerException
	{
		if (null == serviceClientFactory)
		{
			throw new NullPointerException();
		}
		this.serviceClientFactory = serviceClientFactory;
		authorizationManager = new AuthorizationManagerServiceClient(
				serviceClientFactory);
	}

	public List<Role> allRoles()
	{
		// TODO Auto-generated method stub
		return null;
	}

	public void assignRole(String roleId, List<String> principalOids)
			throws UMException
	{
		int size = principalOids.size();
		String[] strings = new String[size];
		for (int i = 0; i < size; i++)
		{
			strings[i] = principalOids.get(i);
		}
		authorizationManager.assignRole(roleId, strings);
	}

	public void createRole(Role role) throws UMException
	{
		RoleImp roleImp = (RoleImp) role;
		authorizationManager.createRole(roleImp.getRole());
	}

	public void createRole(Role role, List<String> addPermissionOids)
			throws UMException
	{
		RoleImp roleImp = (RoleImp) role;
		authorizationManager.createRole(roleImp.getRole(), addPermissionOids);
		// TODO Auto-generated method stub

	}

	public void deleteRole(String roleID) throws UMException
	{
		authorizationManager.deleteRole(roleID);
	}

	public RoleImp findRole(String roleId) throws NullPointerException,
			UMException
	{

		return new RoleImp(authorizationManager.findRole(roleId));
	}

	public List<Role> findRoles(RoleSearchFilter filter)
	{
		// TODO Auto-generated method stub
		return null;
	}

	public Set<Role> findRolesForPrincipal(String principalOid)
			throws UMException
	{
		Set set = authorizationManager.findRolesForPrincipal(principalOid);
		Set<Role> roleSet = new HashSet<Role>();
		Iterator iterator = set.iterator();
		while (iterator.hasNext())
		{
			com.adobe.idp.um.api.infomodel.Role role = (com.adobe.idp.um.api.infomodel.Role) iterator
					.next();
			roleSet.add(new RoleImp(role));
		}
		return roleSet;
	}

	public boolean isUserInRole(String roleID, String principalOid)
			throws UMException
	{
		return authorizationManager.isUserInRole(roleID, principalOid);
	}

	public void unassignRole(String roleId, List<String> userOid)
			throws UMException
	{
		int size = userOid.size();
		String[] strings = new String[size];
		for (int i = 0; i < size; i++)
		{
			strings[i] = userOid.get(i);
		}
		authorizationManager.unassignRole(roleId, strings);
	}

	public void setServiceClientFactory(
			ServiceClientFactory serviceClientFactory)
	{
		this.serviceClientFactory = serviceClientFactory;
		this.authorizationManager = new AuthorizationManagerServiceClient(
				serviceClientFactory);
	}

	public ServiceClientFactory getServiceClientFactory()
	{
		return serviceClientFactory;
	}

	public void unassignRoles(List<String> roleList, List<String> userOidList)
			throws UMException
	{
		int rolesize = roleList.size();
		int usersize = userOidList.size();
		String[] roleStrings = new String[rolesize];
		String[] userStrings = new String[usersize];
		for (int i = 0; i < rolesize; i++)
		{
			roleStrings[i] = roleList.get(i);
		}
		for (int j = 0; j < usersize; j++)
		{
			userStrings[j] = userOidList.get(j);
		}
		authorizationManager.unassignRoles(roleStrings, userStrings);
	}

	public void assignRoles(List<String> roleId, List<String> principalOids)
			throws UMException
	{
         int rolesize=roleId.size();
         int principalsize=principalOids.size();
         String[] roleStrings=new String[rolesize];
         String[] principalStrings=new String[principalsize];
         for(int i=0;i<rolesize;i++)
         {
        	roleStrings[i]=roleId.get(i); 
         }
         for(int j=0;j<principalsize;j++)
         {
           principalStrings[j]=principalOids.get(j); 
         }
         authorizationManager.assignRoles(roleStrings, principalStrings);
	}

}
